import Vue from "vue";
import VueRouter from "vue-router";
import Login from "@/views/Login";
import Layout from "@/layout/Layout";
import Home from "@/views/Home";
import PersonInfo from "@/views/PersonInfo";
import ChangePassword from "@/views/ChangePassword";
import StudentManager from "@/views/administrator/StudentManager";
import TeacherManager from "@/views/administrator/TeacherManager";
import AdministratorManager from "@/views/administrator/AdministratorManager";
import DepartmentManager from "@/views/administrator/DepartmentManager";
import MajorManager from "@/views/administrator/MajorManager";
import CategoryManager from "@/views/administrator/CategoryManager";
import CourseManager from "@/views/administrator/CourseManager";
import ClassesManager from "@/views/administrator/ClassesManager";
import Index from "@/views/Index";
import ClassroomManager from "@/views/administrator/ClassroomManager";
import BuildingManager from "@/views/administrator/BuildingManager";
import TimetableManager from "@/views/administrator/TimetableManager";
import TestPage from "@/views/TestPage";
import MyLesson from "@/views/teacher/MyLesson";
import MyTimetable from "@/views/student/MyTimetable";
import MyCourse from "@/views/student/MyCourse";
import MyResult from "@/views/student/MyResult";
import NotifyManager from "@/views/administrator/NotifyManager";
import ResultInput from "@/views/teacher/ResultInput";
import GradePage from "@/views/teacher/GradePage";

Vue.use(VueRouter);

const routes = [
    {
        path:'/login',
        component:Login,
    },
    {
        path:'/',
        component:Layout,
        children:[
            {
                path:'/',
                component: Home
            },
            {
                path:'/index',
                component: Index
            },
            {
                path:'/personInfo',
                component: PersonInfo
            },
            {
                path: '/changePassword',
                component: ChangePassword
            },
            {
                path: '/student',
                component: StudentManager
            },
            {
                path: '/teacher',
                component: TeacherManager
            },
            {
                path: '/administrator',
                component: AdministratorManager
            },
            {
                path: '/department',
                component: DepartmentManager
            },
            {
                path: '/major',
                component: MajorManager
            },
            {
                path: '/category',
                component: CategoryManager
            },
            {
                path: '/course',
                component: CourseManager
            },
            {
                path: '/class',
                component: ClassesManager
            },
            {
                path: '/classroom',
                component: ClassroomManager
            },
            {
                path: '/building',
                component: BuildingManager
            },
            {
                path: '/myTimetable',
                component: MyTimetable
            },
            {
                path: '/myLesson',
                component: MyLesson
            },
            {
                path: '/timetable',
                component: TimetableManager
            },
            {
                path: '/test',
                component: TestPage
            },
            {
                path: '/myCourse',
                component: MyCourse
            },
            {
                path: '/myResult',
                component: MyResult
            },
            {
                path: '/notify',
                component: NotifyManager
            },
            {
                path: '/resultInput',
                component: ResultInput
            },
            {
                path: '/gradePage/:classesId/:courseId/:year',
                name: 'gradePage',
                component: GradePage
            }
        ]
    }
]

const router =  new VueRouter({
    mode: 'history',
    routes
});

// 未登录拦截
// 挂载路由导航守卫
// to 将要访问的路径
// from 代表从哪个路径跳转而来
// next 是个函数，表示放行 next() 放行  next('/login') 强制跳转
router.beforeEach((to, from, next) => {
    if (to.path.startsWith('/login')) {
        sessionStorage.removeItem('user');
        next();
    } else {
        let user = sessionStorage.getItem('user');
        if (!user) {
            next({
                path: '/login'
            });
        } else {
            next();
        }

        if (to.path.endsWith('/')){
            if (JSON.parse(user).identity!=='管理员'){
                next({
                    path: '/index'
                })
            }
        }

        if (to.path.endsWith('/index')){
            if (JSON.parse(user).identity==='管理员'){
                next({
                    path: '/'
                })
            }
        }
    }

});

export default router;


